package com.shisfish.library.Controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONArray;

import com.shisfish.library.Impl.BookMoreImpl;
import com.shisfish.library.Model.BookMore;
import com.shisfish.library.Utils.ExcelToBean;
import com.shisfish.library.Utils.JsonValueUtil;
import com.shisfish.library.Utils.FileUpload.FileUploadUtils;
import com.shisfish.library.Utils.FileUpload.FormJsonArrUtil;
import com.shisfish.library.Utils.Global.GlobalVariable;

@Path("bookMore")
public class BookMoreController extends GlobalVariable {
	
	@POST
	@Path("insertOne")
	@Produces("application/json; charset=utf-8")
	public String insertOne(@FormParam("ISBN") String ISBN, @FormParam("number") String number, 
			@FormParam("key_word") String key_word, @FormParam("subject_complex") String subject_complex, 
			@FormParam("form_complex") String form_complex, @FormParam("geography_comnplex") String geography_comnplex, 
			@FormParam("logical_number") String logical_number, @FormParam("logical_name") String logical_name, 
			@FormParam("printing_year") String printing_year, @FormParam("binding_and_layout") String binding_and_layout, 
			@FormParam("responsible_remarks") String responsible_remarks, 
			@FormParam("object_remarks") String object_remarks, @FormParam("edition") String edition){
		BookMore bookMore = new BookMore(ISBN, number, key_word, subject_complex, form_complex, geography_comnplex, logical_number, logical_name, printing_year, binding_and_layout, responsible_remarks, object_remarks, edition);
		
		int flag = new BookMoreImpl().insertOne(bookMore);
		
		System.out.println(flag);
		
		return flag+"";
	}
	
	@POST
	@Path("excelInsert")
	@Consumes({MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON})
	@Produces(MediaType.TEXT_PLAIN)
	public String excelInsert(@Context HttpServletRequest request) {
		FileUploadUtils fileUploadUtils = new FileUploadUtils();
		
		JsonValueUtil jsonValueUtils = new JsonValueUtil();
		
		String filePath = "d:\\";
		
		fileUploadUtils.setFilePath(filePath);
		
		// 取得所有文件和表单值 并保存文件至本地
		JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
		
		// 获取文件
		JSONArray fileJsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "file");
		
		// 取得文件保存路径 "XXX" 为前台表单name 值必须一样
		String excelFilePath = filePath + jsonValueUtils.getValue(fileJsArr, "form_picture");

		// 将上传的表格转换成List
		String configFilePath = request.getSession().getServletContext().getRealPath("/") + File.separator + "excelModel/bookMore.xml";
		
		List<BookMore> bookMores = new ArrayList<BookMore>();
		
		ExcelToBean<BookMore> util = new ExcelToBean<BookMore>();
		
		try {
			
			// 判断文件格式是否正确
			bookMores = util.parseExcelFileToBeans(new File(excelFilePath), new File(configFilePath));
			
			int flag = new BookMoreImpl().insertAll(bookMores);
			
			System.out.println(flag);
			
		} catch (Exception e) {
			return "{\"message\":\"excel 表格有误\",\"statusCode\": \"500\"}";
		}
		
		return "{\"message\":\"success\",\"statusCode\": \"200\"}";
		
	}
}
